﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Web.Configuration;
using System.IO;
using System.Net.Configuration;

namespace RioStudio.Website.View
{
    public partial class Contact : RioPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["EmailSession"] != null)
                {
                    var sessionObject = (EmailSession)Session["EmailSession"];

                    var minute = DateTime.Now.Subtract(sessionObject.SendTime);

                    if (minute.Minutes < 16)
                    {
                        btnSend.Enabled = false;
                    }
                }
            }
        }

        protected void btnSend_Click(object sender, ImageClickEventArgs e)
        {
            if (Page.IsValid)
            {
                var fromMail = txtEmail.Text;
                var subject = txtTitle.Text;
                var body = txtContent.Text;
                var toEmail = string.Empty;
                var name = txtFullname.Text;

                var config = WebConfigurationManager.AppSettings["Email"];
                var emailContent = string.Empty;

                using (StreamReader sr = File.OpenText(Server.MapPath("~/Content/email.txt")))
                {
                    emailContent = sr.ReadToEnd();
                }

                if (config != null)
                {
                    toEmail = config.ToString();
                }

                try
                {
                    emailContent = emailContent.Replace("###Email###", fromMail);
                    emailContent = emailContent.Replace("###Name###", name);
                    emailContent = emailContent.Replace("###Content###", body);
                    emailContent = emailContent.Replace("###Phone###", txtTelephone.Text);

                    System.Net.Mail.SmtpClient smtp = new SmtpClient();

                    System.Net.Mail.MailMessage msg = new MailMessage(WebConfigurationManager.AppSettings["AdminEmail".ToLower()].ToString(), toEmail, subject, emailContent);
                    msg.IsBodyHtml = false;
                    smtp.Send(msg);

                    txtContent.Text = string.Empty;
                    txtTitle.Text = string.Empty;
                    txtEmail.Text = string.Empty;
                    txtFullname.Text = string.Empty;
                    txtTelephone.Text = string.Empty;
                    btnSend.Enabled = false;

                    if (Session["EmailSession"] != null)
                    {
                        Session["EmailSession"] = new EmailSession() { IPAddress = HttpContext.Current.Request.UserHostAddress };
                    }
                    else
                    {
                        Session.Add("EmailSession", new EmailSession() { IPAddress = HttpContext.Current.Request.UserHostAddress });
                    }

                    ScriptManager.RegisterStartupScript(this, this.GetType(), "SendContact", "alert('Xin cám ơn bạn đã gửi liên hệ, chúng tôi sẽ trả lời bạn ngay khi nhận được liên hệ!');", true);
                }
                catch (Exception)
                {

                    throw;
                }
            }

        }
    }


    public class EmailSession
    {
        public EmailSession()
        {
            SendTime = DateTime.Now;
        }

        public string IPAddress { get; set; }

        public DateTime SendTime { get; set; }
    }
}